<?php
include_once "checkLogin.php";

?>
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>会员管理系统</title>
    <style>
        .main {
            width: 80%;
            margin: 0 auto;
            text-align: center;
        }

        h2 a {
            margin-right: 15px;
            color: aquamarine;
            text-decoration: none;
        }

        h2 a:hover {
            color: coral;
        }

        h2 a:last-child {
            margin-right: 0;
        }

.red{
    color: red;
}
    </style>
</head>

<body>
<div class="main">
    <?php
    include_once 'nav.php';
    include_once 'conn.php';
    $userID = $_GET['userID']?? '';
    if ($userID){
        $page = $_GET['page'];
        $sql = "select * from userinfo where id = $userID";
    }else{
        $sql = "select * from userinfo where username = '".$_SESSION['loggedUsername']."'";
    }

    $result = mysqli_query($conn,$sql);
    if(mysqli_num_rows($result)){
        $userinfo = mysqli_fetch_array($result,MYSQLI_ASSOC);
        $fav = explode(",",$userinfo['fav']);
    }else{

        die("未找到有效用户！");
    }

    ?>
    <form action="postModify.php" method="post" onsubmit="return check()">
        <table align="center" width="80%" border="1" bordercolor="gray" cellspacing="0" cellpadding="10"
               style="border-collapse: collapse">
            <tr>
                <td align="right">用户名</td>
                <td align="left"><input name="username" readonly value="<?php echo $userinfo['username']; ?>"></td>

            </tr>
            <tr>
                <td align="right">密码</td>
                <td align="left"><input type="password" name="pw" placeholder="不修改密码请留空"></td>
            </tr>
            <tr>
                <td align="right">确认密码</td>
                <td align="left"><input type="password" name="cpw" placeholder="不修改密码请留空"></td>
            </tr>
            <tr>
                <td align="right">邮箱</td>
                <td align="left"><input name="email" value="<?php echo $userinfo['email']; ?>"></td>
            </tr>

            <tr>
                <td align="right"><input type="submit" value="提交"></td>
                <td align="left"><input type="reset" value="重置"></td>
                <input type="hidden" name="page" value="<?php echo $page;?>">
            </tr>
        </table>
    </form>


</div>
</body>
<script>
function check(){
    let pw  = document.getElementsByName('pw')[0].value.trim();
    let cpw = document.getElementsByName('cpw')[0].value.trim();
    let email = document.getElementsByName('email')[0].value.trim();
    //用户名验证
   let pwreg = /^[a-zA-Z0-9_*]{6,10}$/;
   if(pw.length>0){
       if(!pwreg.test(pw)){
           alert('密码必填，且只能大小写字符和数字，以及*号和_构成，长度6-10个字符');
           return false;
       }else {
           if(pw!=cpw){
               alert('确认密码不相符！');
               return false;
           }
       }
   }
   // let emailReg = /[^a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
    let emailReg = /^\w{3,12}@\w{1,5}\.[a-z]{2,3}$/;
   if(email.length>0){
       if(!emailReg.test(email)){
           alert('邮箱格式不正确');
           return false;
       }
   }

return true;
}
</script>

</html>